package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.qo.SystemDictionaryQuery;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryServiceImpl;


    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionary:list", "部门列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryQuery qo) {
        boolean admin = SecurityUtils.getSubject().hasRole("admin");
        SecurityUtils.getSubject().checkPermission("systemDictionary:list");
        PageInfo<SystemDictionary> pageInfo = systemDictionaryServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);
        return "/systemDictionary/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"systemDictionary:delete", "部门删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        systemDictionaryServiceImpl.delete(id);
        return new JsonResult("删除成功", true);

    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionary:saveOrUpdate", "部门新增编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary, Long id) {
        if (id != null) {
            systemDictionaryServiceImpl.update(systemDictionary);
        } else {
            systemDictionaryServiceImpl.save(systemDictionary);
        }
        return new JsonResult("新增/编辑成功", true);
    }
}
